<?php

// global info
$host = 'localhost';
$user = 'TIEI';
$pass = 'Tiei23IWP';


function getPDO()
{
    global $host;
    global $user;
    global $pass;
    $db = 'quizme';
    $charset = 'utf8';

    $dsn = "mysql:host=$host;dbname=$db;charset=$charset";
    $options = [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES => false,
    ];
    try {
        return new PDO($dsn, $user, $pass, $options);
    } catch (\PDOException $e) {
        throw new \PDOException($e->getMessage(), (int) $e->getCode());
    }
}


function createDB()
{
    global $host;
    global $user;
    global $pass;
    try {
        $conn = new PDO("mysql:host=$host", $user, $pass);

        // 设置 PDO 错误模式为异常
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "CREATE DATABASE quizme";

        // 使用 exec() ，因为没有结果返回
        $conn->exec($sql);

        echo "数据库创建成功<br>";
    } catch (PDOException $e) {
        throw $e;
    }
}

?>